class Element {
  constructor(element, attrs, innerText) {
    this.tag = element
    attrs?this.attrs = attrs:""
    innerText?this.innerText = innerText:""
  }
}
class CheckElement {
  constructor(element) {

  }
  isFromElement(element) {
    return ['input']
  }
  isSingleElement() {

  }
}
class Check {
  isElementClass(element) {
    return element instanceof Element
  }
  isBasicType(element) {
    return typeof element != Object
  }
}
class ElementObject {
  constructor(element, elementObjs) {
    if (this.check(element)) {
      this.element = element
      if (elementObjs) {
        this.elementObjs =this.elementsToElementObj(elementObjs)
      }
    } else {
      return new Error("不能传入非Element对象")
    }

  }
  check(element) {
    return this.checks([element])
  }
  checks(elements) {
    if (elements == undefined) {
      return true
    }
    let check = new Check()
    for (let i = 0; i < elements.length; i++) {
      if (!check.isElementClass(elements[i]) ? true : false) {
        return false;
      }
    }
    return true
  }
  elementsToElementObj(elements){
	if(elements instanceof Array){
		return elements.map(x=>{
			return x instanceof ElementObject?x:new ElementObject(x)
		})
	}else{
		return elements instanceof ElementObject?elements:new ElementObject(elements)
	}
  }
}
/**
 * dom渲染器
 */
class Render {
  constructor(elementObj) {
    this.Node=this.render(elementObj)
  }
  /** 检查是否是单标签
   * @param {Object} tag 标签
   */
  checkRender(tag){
	  return ['br','hr','img','param','meta','link'].indexOf(tag.toLowerCase())>-1
  }
  /** 
   * @param {Object} elementObj 渲染树 
   */
  render(tree) {
		/* //解析1 渲染10w条数据，耗时5s
		if(tree){
			var tag=document.createElement(tree.tag.at(0))
			if(tree.tag.at(1)){
				Object.entries(tree.tag.at(1)).forEach(value=>{
					tag.setAttribute(value[0],value[1])
				})
			}
			if(tree.tag.at(2)){
				tag.innerHTML=tree.tag.at(2)
			}
			if(tree.child instanceof Array){
				tree.child.forEach(x=>{
					tag.appendChild(this.render(x))
				})
			}
		} */
		//解析2 渲染只需2s多一点
		if(tree){
			var tag=`<${tree.tag.at(0)} `
			var isSingle=this.checkRender(tag)
			//console.log(tree.tag.at(1))
			if(tree.tag.at(1)){
				Object.entries(tree.tag.at(1)).forEach(value=>{
					tag+=`${value[0]}='${value[1]}'`
				})
			}
			tag=isSingle?tag+' />':tag+">"
			if(tree.tag.at(2)){
				tag=isSingle?"":tag+tree.tag.at(2)
			}
			if(tree.child instanceof Array){
				tree.child.forEach(x=>{
					tag=tag+this.render(x)
					return tag
				})
			}
			return tag+(!isSingle?"</"+tree.tag.at(0)+">":"")
		}
	}
  mouted=function(element){
	let mydom=element instanceof HTMLElement?element:document.querySelector(element)
	if(this.Node instanceof HTMLCollection){
		mydom.appendChild(this.Node)
	}else{
		mydom.innerHTML=this.Node
		}
	}
}